#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     mk
   Description:
   Author:        joshua
   date:          2018/12/21
-------------------------------------------------
"""
__author__ = 'joshua'

from os import environ as ENVIRON
from ctypes import CDLL
import time


# 判断系统是否为64位系统
def is64Windows():
    return 'PROGRAMFILES(X86)' in ENVIRON


# 驱动级键鼠控制类
class MKController(object):
    def __init__(self):
        super(MKController, self).__init__()
        # 64位系统加载64位的dll
        if is64Windows():
            self.ctrl = CDLL("resources/lib/DD64.dll")
        else:
            self.ctrl = CDLL("resources/lib/DD32.dll")

    # 鼠标点击
    def mouse_click(self, x=None, y=None, type="left", count=1):
        '''
        参数： 1 =左键按下 ，2 =左键放开
               4 =右键按下 ，8 =右键放开
               16 =中键按下 ，32 =中键放开
        :param x:x坐标
        :param y:y坐标
        :param type:按键类型:left,right,middle
        :param count:次数:1~n
        :return: None
        '''
        type_map = dict(left=[1, 2], middle=[16, 32], right=[4, 8])
        type = type_map[type]
        while count > 0:
            if x and y:
                self.ctrl.DD_mov(x, y)
            self.ctrl.DD_btn(type[0])
            self.ctrl.DD_btn(type[1])
            count -= 1

    # 键盘敲击字符串
    def key_type(self, x=None, y=None, text=""):
        '''
        :param x:x坐标
        :param y:y坐标
        :param text:需输入的字符串(不含中文)
        :return: None
        '''
        if x and y:
            self.mouse_click(x, y)
            time.sleep(1)
        chs = list(text)
        for c in chs:
            self.ctrl.DD_str(c)

    #清空输入框
    def clear_input(self,x,y):
        self.mouse_click(x, y)
        time.sleep(1)
        count=30
        while count>0:
            self.ctrl.DD_mov(x,y)
            self.ctrl.DD_key(214,1)
            self.ctrl.DD_key(214,2)
            count-=1
        count=30
        while count>0:
            self.ctrl.DD_mov(x, y)
            self.ctrl.DD_key(706,1)
            self.ctrl.DD_key(706,2)
            count-=1

    def press_key(self, code,type):
        self.ctrl.DD_key(code, type)


if __name__ == "__main__":
    mk = MKController()
    mk.clear_input(1674,440)